package com.leapp.sk.viewdelegate.personal

import android.support.v4.content.ContextCompat
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import android.widget.ImageView
import com.example.baselibrary.util.LKToastUtil
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.ContainsEmojiEditText
import com.example.mvp.view.SKTopBar
import com.leapp.sk.R
import com.leapp.sk.inter.TopBarRightClickInterface
import com.leapp.sk.ui.activity.personal.SettingNickActivity

/**
 * 设置昵称视图托管类
 * @author  chaohaipeng  on 2018/8/8.
 */
class SettingNickDelegate: BaseAppDelegate(){
    var topBarRightClickInterface: TopBarRightClickInterface?=null
    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_setting_nick

    }

    /**
     * 初始化文本内容
     */
    fun initEditText(nick:String?,topBarRightClickInterface: TopBarRightClickInterface){
        this.topBarRightClickInterface=topBarRightClickInterface
        val et_asn_nick_content = getView<ContainsEmojiEditText>(R.id.et_asn_nick_content)
        if (!TextUtils.isEmpty(nick)){
            et_asn_nick_content.setText(nick)
        }
        initEvent()
    }

    /**
     * 初始化事件
     */
    private fun initEvent(){
        val et_asn_nick_content = getView<ContainsEmojiEditText>(R.id.et_asn_nick_content)
        val iv_asn_clear = getView<ImageView>(R.id.iv_asn_clear)
        et_asn_nick_content.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(p0: Editable?) {
                val toString = p0?.toString()
                val compareTo = toString?.length!!.compareTo(0)
                if(compareTo==0){
                    iv_asn_clear.visibility= View.INVISIBLE
                }else{
                    iv_asn_clear.visibility= View.VISIBLE
                }
            }
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }
            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }
        })

    }

    /**
     * 获取提交内容
     */
    fun getSubmitContent():String{
        val et_asn_nick_content = getView<ContainsEmojiEditText>(R.id.et_asn_nick_content)
        val text = et_asn_nick_content.text
        return text.toString()
    }

    /**
     * 设置标题栏
     */
    fun setTitle(title:String?){
        val jtb_asn= getView<SKTopBar>(R.id.jtb_asn)
        if (!TextUtils.isEmpty(title)){
            jtb_asn.setTitle(title)
        }
        setEvent(jtb_asn)
    }

    /**
     * 设置设置事件
     */
    fun setEvent(topBar:SKTopBar){
        topBar.setRightTVContent(getActivity<SettingNickActivity>().resources.getString(R.string.string_class_submit))
        topBar.setRightTVColor(ContextCompat.getColor(getActivity<SettingNickActivity>(), R.color.color_FF5894))
        topBar.getTv_ljtb_right_back()?.visibility= View.VISIBLE
        topBar?.setEventInterface(object : SKTopBar.EventInterface{
            override fun leftOnClick() {
                getActivity<SettingNickActivity>().finish()

            }
            override fun rightOnClick() {
                if(TextUtils.isEmpty(getSubmitContent())){
                    LKToastUtil.showToastLong(getActivity<SettingNickActivity>().resources.getString(R.string.string_setting_hint_nick))
                    return
                }
                topBarRightClickInterface?.searchData()

            }

        })
    }
}